CREATE OR REPLACE FUNCTION public.workflowuser(
    p_bill character varying,
    p_billid integer,
	p_billtype integer,
	p_orgunitid integer,
    p_org_id integer)
  RETURNS json AS
$BODY$
declare  
    v_workflowid integer;  ----工作流程ID
    v_workflow_stateid  integer;  ----工作流程状态ID
    v_next_workflow_stateid  integer=0;  ----下一工作流程状态ID
    v_workflow_transitionid integer;---工作流程
	v_workflow_bill_stateid integer;
    v_cnt integer;
    v_sql text;
    v_data text;
begin
    select COALESCE(max(id),0) into v_workflow_bill_stateid 
	From public.workflow_bill_state where org_id=p_org_id and bill_enum=p_bill and billid=p_billid and bill_type=p_billtype and is_active=true;

	if v_workflow_bill_stateid =0 then
			---插入单据状态
		select t1.id into v_workflow_stateid
		from public.workflow_state t1 where t1.org_id=p_org_id and t1.docstatus=0;
	
		select count(id) into v_cnt
	    From workflow where org_id =p_org_id and is_active=true and bill_enum=p_bill  and bill_type=p_billtype and orgunitid=p_orgunitid ;
	    if v_cnt>1 then--活动审批流有多个
		   v_workflowid :=0;
		elsif v_cnt=1 then
			select COALESCE(max(id),0)into v_workflowid
        	From workflow where org_id =p_org_id and is_active=true and bill_enum=p_bill  and bill_type=p_billtype and orgunitid=p_orgunitid ;
		else
			select COALESCE(max(id),0)into v_workflowid
			From workflow where org_id =p_org_id and is_active=true and bill_enum=p_bill  and bill_type=p_billtype
			and (orgunitid=0 or orgunitid in(select id From orgunit where position(code in (select code from orgunit where id= p_orgunitid))=1));
	    end if;

	else
		select workflowid into v_workflowid
		From public.workflow_bill_state where id=v_workflow_bill_stateid;
		select workflow_stateid into v_workflow_stateid
		From public.workflow_bill_state where id=v_workflow_bill_stateid;
	end if;

	v_sql := 'select COALESCE(min(id),0) from  
	(select public.workflowtabsel(''' || p_bill || ''',condition,''id'',''id'',''' || p_billid || ''') as billid , id 
	from  public.workflow_transition where workflowid=' || v_workflowid || ' and workflow_stateid=' || v_workflow_stateid || ' and next_type= 1) aa
	where aa.billid>0';
	execute  v_sql into v_workflow_transitionid; 


		select next_workflow_stateid into v_next_workflow_stateid
		From public.workflow_transition where id=v_workflow_transitionid ;
		v_sql := 'select COALESCE(min(id),0) from  
		(select public.workflowtabsel(''' || p_bill || ''',condition,''id'',''id'',''' || p_billid || ''') as billid , id 
		from  public.workflow_transition where workflowid=' || v_workflowid || ' and workflow_stateid=' || v_next_workflow_stateid || ' and next_type= 1) aa
		where aa.billid>0';
		execute  v_sql into v_workflow_transitionid; 
		
	   select array_to_json(array_agg(row_to_json(t))) into v_data from (
		  select t2.id,t2.code,t2.name
		from  public.workflow_transition t1,public.users t2
		where t1.id= v_workflow_transitionid and ((t2.id = any(t1.allow_user)) 
		or (t2.groups && t1.allow_group)) ) t;
	   return v_data::json; 

END;  
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;